package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee , HttpSession session){


        session.setAttribute("employee" , new Employee());

        Employee e = (Employee) session.getAttribute("employee");




        //1. 创建条件对象
        LambdaQueryWrapper<Employee > lqw = new LambdaQueryWrapper<>();

        //1.1 设置条件
        lqw.eq(Employee::getUsername , employee.getUsername());

        //先加密密码：
        String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        //使用加密的密码去查询即可
        lqw.eq(Employee::getPassword , pwd);

        //2. 执行查询
        Employee loginEmployee = employeeService.getOne(lqw);

        //3. 判断结果：
        if(loginEmployee != null){ //能查到用户！

            //把这个登录查询出来的员工对象，保存到session作用域里面去!
            session.setAttribute("employee" , loginEmployee );


            //以前的代码：
            //return  new R(1 , loginEmployee , "登录成功！");
            //现在的写法：
            return R.success(loginEmployee);
        }
        return R.error("用户名或者密码错误！");
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){

        //1. 把session的内容清空了即可
        //session.removeAttribute("employee");

        // 让session失效。
        session.invalidate();

        //2. 返回结果
        return R.success("退出成功！");

    }


    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee , HttpSession session){

        //1. 补充数据
        //设置初始密码是： 123456
        String pwd = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(pwd);

        //employee.setCreateTime(LocalDateTime.now()); //设置创建时间
        //employee.setUpdateTime(LocalDateTime.now()); //设置更新时间

        //Employee e = (Employee) session.getAttribute("employee");

        // 谁登录，就是谁执行的操作。
        //employee.setCreateUser(e.getId());  //设置是谁创建的这个用户
        //employee.setUpdateUser(e.getId()); //设置是谁更新的这个用户

        //2. 添加操作
        if(employeeService.save(employee)){
            return R.success("添加成功！");
        }
        return R.error("添加失败！");
    }

    /**
     * 员工分页
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R page(PageParam pageParam){

        //1. 构建条件对象
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        //1.1 设置条件:: 如果name 有值，就追加条件，否则就不追加。
        lqw.like(pageParam.getName() != null , Employee::getName , pageParam.getName());


        //2. 构建分页对象:: 设置查询第几页，每页查询多少条
        IPage<Employee> page = new Page<>(pageParam.getPage() , pageParam.getPageSize());

        //3. 执行查询
        employeeService.page(page , lqw);

        //返回结果
        return R.success(page);
    }

    /**
     * 更新员工
     * @param employee
     * @return
     */
    @PutMapping
    public R update (@RequestBody Employee employee , HttpSession session){

        //1. 补充数据
        //Employee e = (Employee) session.getAttribute("employee");
        //employee.setUpdateUser(e.getId()); //设置是谁更新的
        //employee.setUpdateTime(LocalDateTime.now()); //设置更新的时间

        //2. 执行更新。
        boolean result = employeeService.updateById(employee);

        //3. 判定结果
        if(result){
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }

    /**
     * 根据id来查询员工数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){

        //1. 调用service
        Employee employee = employeeService.getById(id);

        //2. 返回结果
        return R.success(employee);
    }
}
